package com.toe.forum.webapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.pojo.domain.ToeAuthenticationInfo;
import com.toe.common.restful.StateEnum;
import com.toe.forum.service.ForumReplyService;
import com.toe.common.censor.TextCensor;
import com.toe.forum.webapi.mapper.ForumReplyMapper;
import com.toe.pojo.dto.forum.ReplyAddDTO;
import com.toe.pojo.entity.forum.ForumReply;
import com.toe.pojo.vo.forum.ReplyVO;
import com.toe.pojo.vo.user.UserUserVO;
import com.toe.sso.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class ForumReplyServiceImpl extends ServiceImpl<ForumReplyMapper, ForumReply> implements ForumReplyService {
    @DubboReference
    private IUserInfoService dubboUserInfoService;

    @Autowired
    private ForumReplyMapper forumReplyMapper;

    @Override
    public void insert(ReplyAddDTO replyAddDTO) {
        log.info("开始处理添加回复的业务");
        String message = replyAddDTO.getRemark();
        if (message==null){
            throw new ToeServiceException(StateEnum.ERR_FORBIDDEN,"请输入评论内容!");
        }
        String violationReason = TextCensor.TextCensor(message);
        if (violationReason!=null){
            throw new ToeServiceException(StateEnum.ERR_FORUM_VIOLATION,violationReason);
        }
        ForumReply forumReply = new ForumReply();
        BeanUtils.copyProperties(replyAddDTO,forumReply);
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        ToeAuthenticationInfo toeAuthenticationInfo = (ToeAuthenticationInfo) credentials;
        Long userId = toeAuthenticationInfo.getId();
        UserUserVO user = dubboUserInfoService.findUserById(userId);
        String nickname = user.getNickname();
        String url = user.getAvatar();
        forumReply.setUserId(userId);
        forumReply.setNickname(nickname);
        forumReply.setUrl(url);
        forumReply.setReplyName(replyAddDTO.getReplyName());
        forumReply.setReplyUrl(replyAddDTO.getReplyUrl());
        LocalDateTime date = LocalDateTime.now();
        forumReply.setCreateTime(date);
        forumReply.setUpdateTime(date);
        forumReply.setIsReport(0);
        forumReply.setLabel(2);
        forumReplyMapper.insert(forumReply);
        log.info("该回复添加完成!!!!!!!!!!!");
    }

    @Override
    public void delete(long id) {
        forumReplyMapper.deleteById(id);
    }

    @Override
    public List<ReplyVO> selectReplyByRemarkId(long remarkId) {
        List<ReplyVO> replyVOS = forumReplyMapper.selectReplyByRemarkId(remarkId);
        return replyVOS;
    }
}
